package com.itheima.reggie.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SetmealController {
        @Autowired
        private SetmealService setmealService;

        // 套餐分页查询
        @GetMapping("/setmeal/page")
        public ResultInfo findByPage(
                String name,
                @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {// 1.接收参数
            // 2.调用service查询
            Page<Setmeal> page = setmealService.findByPage(pageNum, pageSize, name);

            // 3.返回结果
            return ResultInfo.success(page);
        }

    // 套餐新增
    @PostMapping("/setmeal")
    public ResultInfo save(@RequestBody Setmeal setmeal) { // 1.接收参数
        // 2.调用service
        setmealService.save(setmeal);

        // 3.返回结果
        return ResultInfo.success(null);

    }

    // 套餐删除
    @DeleteMapping("/setmeal")
    public ResultInfo deleteBatchIds(@RequestParam List<Long> ids) { // 1.接收参数
        // 2.调用serivce删除
        if (CollectionUtil.isNotEmpty(ids)) {
            setmealService.deleteBatchIds(ids);
        }

        // 3.返回结果
        return ResultInfo.success(null);
    }

    // dish回显
    @GetMapping("/setmeal/{id}") // 前面没有#
    public ResultInfo findById(@PathVariable Long id) { // 1.接收菜品id
        // 2.调用service查询
        Setmeal setmeal = setmealService.findById(id);
        // 3.返回结果
        return ResultInfo.success(setmeal);
    }

    // setmeal修改
    @PutMapping("/setmeal")
    public ResultInfo update(@RequestBody Setmeal setmeal) { // 1.接收请求体参数
        // 2.调用service修改
        setmealService.update(setmeal);
        // 3.返回成功消息
        return ResultInfo.success(null);

    }

    //setmeal状态更新
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        setmealService.updateStatus(status,ids);
        return ResultInfo.success(null);
    }
}

